package com.tkm.expressions.operators

fun main() {
    "Hello" == "World"
    "Hello".equals("World")

    2 + 3
    2.plus(3)

    val list = listOf(1, 2, 3, 4)
    2 in list
    list.contains(2)

    val map = mutableMapOf(
        "name" to "Bread",
        "age" to 24
    )

    map["name"]
    map.get("name")

    map["age"] = 25
    map.set("age", 25)

    val func = fun(a: Int, b: Int) = a + b
    func(1, 2)
    func.invoke(1, 2)

    2 > 3
    2.compareTo(3)

    //  中缀表达式
    2 to 3
    2.to(3)

    "HelloWorld".rotate(5)
    "HelloWorld" rotate 5

    val book = Book()
    val desk = Desk()
    book.on(desk)
    book on desk
}

//  定义中缀表达式
infix fun String.rotate(count: Int): String {
    val index = count / this.length
    return this.substring(index) + this.substring(0, index)
}

class Desk
class Book

infix fun Book.on(desk: Desk) {
    //  this即为Book实例，也就是receiver
    println("$desk on $this")
}
